package common.exception.parse;

import common.data.commonresponse.CommonResponse;
import common.exception.code.BizErrorCode;
import common.util.CommonResponseUtils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


import javax.validation.ConstraintViolationException;

/**
 * @className: GlobalExceptionHandle
 * @description: 解决什么问题：全局异常处理器，所有的异常在此处理
 * @author: lanyukai
 * @date: 2024/6/5 17:41
 * @since 1.0
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 触发参数校验异常时，打印异常信息并返回异常请求
     * @author lanyukai
     * @date 2024/06/05 17:43
     * @param ex 校验异常
     * @return CommonResponse
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public CommonResponse handleConstraintException(ConstraintViolationException ex){
        log.error("触发校验异常",ex);
        return  CommonResponseUtils.failed(BizErrorCode.PARAM_VALID_FIELD_REQUIRE);
    }

    /**
     * 触发空异常时，打印异常信息并返回异常请求
     * @author lanyukai
     * @date 2024/06/05 17:44
     * @param ex 空指针异常
     * @return CommonResponse
     */
    @ExceptionHandler(NullPointerException.class)
    public CommonResponse handleConstraintException(NullPointerException ex){
        log.error("触发异常空指针异常",ex);
        return CommonResponseUtils.failedWithMsg("0000","空指针异常");
    }

    /**
     * 触发业务异常时，打印异常信息并返回异常请求
     * @author lanyukai
     * @date 2024/06/05 17:44
     * @param ex 业务异常
     * @return CommonResponse
     */
    @ExceptionHandler(BusinessException.class)
    public CommonResponse handleBusinessException(BusinessException ex){
        log.error("触发业务异常",ex);
        return CommonResponseUtils.failedWithMsg(ex.getCode(),ex.getMessage());
    }

    /**
     * 触发系统、网关等底层异常时，打印异常信息并返回异常请求
     * @author lanyukai
     * @date 2024/06/05 17:46
     * @param ex 系统、网关等底层异常
     * @return CommonResponse
     */
    @ExceptionHandler(SystemException.class)
    public CommonResponse handleBusinessException(SystemException ex){
        log.error("触发系统或网关异常",ex);
        return CommonResponseUtils.failedWithMsg(ex.getCode(),ex.getMessage());
    }

    /**
     * 触发API异常时，打印异常信息并返回异常请求
     * @author lanyukai
     * @date 2024/06/05 17:46
     * @param ex api异常
     * @return CommonResponse
     */
    @ExceptionHandler(ThirdPartyApiException.class)
    public CommonResponse handleBusinessException(ThirdPartyApiException ex){
        log.error("触发api异常",ex);
        return CommonResponseUtils.failedWithMsg(ex.getCode(),ex.getMessage());
    }

    /**
     * 触发DAO异常时，打印异常信息并返回异常请求
     * @author lanyukai
     * @date 2024/06/05 17:46
     * @param ex dao异常
     * @return CommonResponse
     */
    @ExceptionHandler(DaoException.class)
    public CommonResponse handleBusinessException(DaoException ex){
        log.error("触发Dao层异常",ex);
        return CommonResponseUtils.failedWithMsg(ex.getCode(),ex.getMessage());
    }

    /**
     * 触发service异常时，打印异常信息并返回异常请求
     * @author lanyukai
     * @date 2024/06/05 17:46
     * @param ex service异常
     * @return CommonResponse
     */
    @ExceptionHandler(ServiceException.class)
    public CommonResponse handleBusinessException(ServiceException ex){
        log.error("触发service层异常",ex);
        return CommonResponseUtils.failedWithMsg(ex.getCode(),ex.getMessage());
    }
}
